#include "calendar.h"


void calendar_init()
{
	//char months[] = ;
	calendar_month[0] = create_ptr( "Januar");
	calendar_month[1] = create_ptr("Februar");
	calendar_month[2] = create_ptr("Mart");
	calendar_month[3] = create_ptr("April");
	calendar_month[4] = create_ptr("May");
	calendar_month[5] = create_ptr("Jule");
	calendar_month[6] = create_ptr("June");
	calendar_month[7] = create_ptr("August");
	calendar_month[8] = create_ptr("September");
	calendar_month[9] = create_ptr("October");
	calendar_month[10] = create_ptr("November");
	calendar_month[11] = create_ptr("December");
	calendar_month_days[0] = 31;
	calendar_month_days[1] = 28;
	calendar_month_days[2] = 31;
	calendar_month_days[3] = 30;
	calendar_month_days[4] = 31;
	calendar_month_days[5] = 30;
	calendar_month_days[6] = 31;
	calendar_month_days[7] = 31;
	calendar_month_days[8] = 30;
	calendar_month_days[9] = 31;
	calendar_month_days[10] = 30;
	calendar_month_days[11] = 31;
	calendar_weekdays[0] = create_ptr("Sun");
	calendar_weekdays[1] = create_ptr("Mon");
	calendar_weekdays[2] = create_ptr("Tue");
	calendar_weekdays[3] = create_ptr("Thr");
	calendar_weekdays[4] = create_ptr("Wnd");
	calendar_weekdays[5] = create_ptr("Fri");
	calendar_weekdays[6] = create_ptr("Sat");
	
	//char days[] = ;
}

void calendar_draw()
{
	time_t loc_time;
	struct tm *loc_date;
	DImage *bg_image=NULL;
	DFont *font=NULL;
	int i,j;
	int time_div,time_mod,first_day,last_day;
	
	//get time routines
	loc_time = time(NULL);
	loc_date = gmtime( (const time_t *)&loc_time );
	
	bg_image = draw_load_image( v_bg_path );
	if (bg_image == NULL)
	{
		printf("Cannot load image\n");
	}
	font = draw_load_font( v_font_path, v_font_size );
	
	if (v_month == TRUE)
		draw_font( bg_image, font, v_pos_x, v_pos_y, v_color, calendar_month[loc_date->tm_mon]);
	if (v_year == TRUE)
	{
		char tmp_str[10];
		sprintf( tmp_str, "%d", 1900+loc_date->tm_year );
		draw_font( bg_image, font, v_pos_x+v_width, v_pos_y, v_color, tmp_str );
	}
	//configure day of month
	for (i=0;i<35;i++) calendar_days[i] = 0xff;
	time_div = (loc_date->tm_wday - loc_date->tm_mday)%7;
	//oh sorry i forget about sunday
	for (i=1;i<=calendar_month_days[loc_date->tm_mon];i++)
	{
		calendar_days[i-1+time_div]=i;
		if (i == loc_date->tm_mday)
		{
			calendar_days[i-1+time_div] += 35;
		}
	}
	first_day = time_div;
	last_day = calendar_month_days[loc_date->tm_mon+time_div-1];
	last_day = calendar_month_days[loc_date->tm_mon+time_div-1];
	for (i=last_day; i<35; i++)
	{
		calendar_days[i]=i-calendar_month_days[loc_date->tm_mon]+70;
	}
	for (i=0; i<time_div;i++)
	{
		calendar_days[i] = calendar_month_days[(loc_date->tm_mon-1)%12]-time_div+i+70;
	}
	
	//draw all days of month
	for (i=0; i<5; i++)
	{
		char tmp_str[3];
		int tmp_i;
		for (j=0; j<7; j++)
		{
			if (calendar_days[i*7+j] != -1)
			{
				tmp_i = calendar_days[i*7+j]/35;
				switch(tmp_i)
				{
					case 0:
						sprintf( tmp_str, "%02d", calendar_days[i*7+j]);
						tmp_str[2] = '\0';
						draw_font( bg_image, font, v_pos_x+j*v_font_size, v_pos_y+v_font_size*i+v_font_size, v_color, tmp_str);
						break;
					case 1:
						sprintf( tmp_str, "%02d", loc_date->tm_mday);
						tmp_str[2] = '\0';
						draw_font( bg_image, font, v_pos_x+j*v_font_size, v_pos_y+v_font_size*i+v_font_size, 0xaa5050, tmp_str);
						break;
					case 2:
						sprintf( tmp_str, "%02d", calendar_days[i*7+j]-69);
						tmp_str[2] = '\0';
						draw_font( bg_image, font, v_pos_x+j*v_font_size, v_pos_y+v_font_size*i+v_font_size, 0xcccccc, tmp_str);
						break;
					default:
						break;
				}
			}
		}
	}
	draw_save_image( bg_image, "save.bmp");
	
	draw_free_surface( bg_image );
}

void calendar_gcollector()
{
	int i;
	for (i=0;i<12;i++)	free( (void *)calendar_month[i] );
	for (i=0;i<7;i++)	free( (void *)calendar_weekdays[i] );
}

const char* create_ptr( const char *name )
{
	int size = strlen(name);
	char *str = malloc( size );
	memset( str, '\0', size );
	strncpy( str, name ,size);
	str[size] = '\0';
	return str;
}
